/*
* Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
*/
#include "format_field.h"
#include "vpc_format_def.h"
/*******************rgb pkt*******************/
static int32_t rgb_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888:
            SET_VPC_PIC_FMT(RGB888_PACKED, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888:
            SET_VPC_PIC_FMT(RGB888_PACKED, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D24, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_22, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_fp16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_FP16:
            SET_VPC_PIC_FMT(RGB888_PACKED_FP16, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_FP16:
            SET_VPC_PIC_FMT(RGB888_PACKED_FP16, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D48, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_fp32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_FP32:
            SET_VPC_PIC_FMT(RGB888_PACKED_FP32, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_FP32:
            SET_VPC_PIC_FMT(RGB888_PACKED_FP32, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D96, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_bf16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_BF16:
            SET_VPC_PIC_FMT(RGB888_PACKED_BF16, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_BF16:
            SET_VPC_PIC_FMT(RGB888_PACKED_BF16, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D48, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_s16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_INT16:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT16, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_INT16:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT16, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D48, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_s32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_INT32:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT32, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_INT32:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT32, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D96, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_u16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_UINT16:
            SET_VPC_PIC_FMT(RGB888_PACKED_UINT16, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_UINT16:
            SET_VPC_PIC_FMT(RGB888_PACKED_UINT16, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D48, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_u32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_UINT32:
            SET_VPC_PIC_FMT(RGB888_PACKED_UINT32, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_UINT32:
            SET_VPC_PIC_FMT(RGB888_PACKED_UINT32, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D96, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

static int32_t rgb_s8_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_INT8:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT8, RGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_INT8:
            SET_VPC_PIC_FMT(RGB888_PACKED_INT8, BGR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D24, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_88, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

/*******************rgb plan*******************/
static int32_t rgb_plan_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR:
            SET_VPC_PIC_FMT(RGB888_PLANNAR, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR:
            SET_VPC_PIC_FMT(RGB888_PLANNAR, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D8, D8, D8);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_22, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_fp16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_FP16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_FP16, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_FP16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_FP16, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D16, D16, D16);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_fp32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_FP32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_FP32, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_FP32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_FP32, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D32, D32, D32);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_bf16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_BF16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_BF16, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_BF16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_BF16, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D16, D16, D16);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_s16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_INT16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_INT16, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_INT16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_INT16, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D16, D16, D16);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_s32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_INT32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_INT32, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_INT32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_INT32, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D32, D32, D32);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_u16_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_UINT16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_UINT16, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_UINT16:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_UINT16, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D16, D16, D16);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_u32_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_UINT32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_UINT32, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_UINT32:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_UINT32, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D32, D32, D32);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_33, NUM_33, NUM_33); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}

static int32_t rgb_plan_s8_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_RGB_888_PLANAR_INT8:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_INT8, BGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGR_888_PLANAR_INT8:
            SET_VPC_PIC_FMT(RGB888_PLANNAR_UINT32, BGR_PLANAR);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D32, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_22, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(RGB_PLANNAR);
    return 0;
}
/*******************rgba pkt*******************/
static int32_t rgba_format_info_init(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t format)
{
    switch (format) {
        case VPC_PIPE_PIXEL_FORMAT_ARGB_8888:
            SET_VPC_PIC_FMT(XRGB8888_PACKED, ARGB);
            break;
        case VPC_PIPE_PIXEL_FORMAT_ABGR_8888:
            SET_VPC_PIC_FMT(XRGB8888_PACKED, ABGR);
            break;
        case VPC_PIPE_PIXEL_FORMAT_RGBA_8888:
            SET_VPC_PIC_FMT(XRGB8888_PACKED, RGBA);
            break;
        case VPC_PIPE_PIXEL_FORMAT_BGRA_8888:
            SET_VPC_PIC_FMT(XRGB8888_PACKED, BGRA);
            break;
        default: return 1;
    }

    SET_PIXEL_FMT(D32, IDEL, IDEL);
    SET_PIXEL_STEP(NUM_1, NUM_1); // xstep/ystep
    SET_DOWN_SAMPLE_COEFF(NUM_13, NUM_65, NUM_100, NUM_65, NUM_13, NUM_0); // 下采样系数
    SET_UVUP_EN(NUM_1); // 滤波开关
    SET_PORT_DU(NUM_22, NUM_11, NUM_11); // DU配置
    SET_PLANN_FMT(BUTT_PLANNAR);
    return 0;
}

int32_t vpc_pipe_init_rgb_field_format_info(vpc_fmt_info_t *fmt_info, vpc_pipe_pic_fmt_t fmt)
{
    CHECK_SUCC_RET(rgb_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_fp16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_fp32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_bf16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_s16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_s32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_u16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_u32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_s8_format_info_init(fmt_info, fmt));

    CHECK_SUCC_RET(rgb_plan_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_fp16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_fp32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_bf16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_s16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_s32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_u16_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_u32_format_info_init(fmt_info, fmt));
    CHECK_SUCC_RET(rgb_plan_s8_format_info_init(fmt_info, fmt));

    CHECK_SUCC_RET(rgba_format_info_init(fmt_info, fmt));

    return 1;
}
